package com.tony.utils;

import java.io.File;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @Auther: tony
 * @Date: 2018-11-30 17:07
 * @Description: https://ai.qq.com/
 */
public class TencentAIUtil {

    /** 文字识别的账号*/
    //private static final String APP_ID="1106763733";
    //private static final String APP_KEY="vMYEnJNYdxHHyApE";

    /** 我自己的账号*/
    private static final String APP_ID="2110172125";
    private static final String APP_KEY="HeUlxoADN34nzPLY";

    //通用
    private static final String General_OCE_URL="https://api.ai.qq.com/fcgi-bin/ocr/ocr_generalocr";
    //手写体
    private static final String HandWriting_OCE_URL="https://api.ai.qq.com/fcgi-bin/ocr/ocr_handwritingocr";
    //营业执照
    private static final String BizLicense_OCE_URL="https://api.ai.qq.com/fcgi-bin/ocr/ocr_bizlicenseocr";
    //银行卡
    private static final String CreditCard_OCE_URL="https://api.ai.qq.com/fcgi-bin/ocr/ocr_creditcardocr";
    //车牌
    private static final String Plate_OCE_URL="https://api.ai.qq.com/fcgi-bin/ocr/ocr_plateocr";
    //名片
    private static final String BC_OCE_URL="https://api.ai.qq.com/fcgi-bin/ocr/ocr_bcocr";
    //身份证
    private static final String IdCard_OCE_URL="https://api.ai.qq.com/fcgi-bin/ocr/ocr_idcardocr";
    //行驶证驾驶证OCR
    private static final String DriverLicense_OCE_URL="https://api.ai.qq.com/fcgi-bin/ocr/ocr_driverlicenseocr";




    public static void main(String[] args){
        onePaceOCR("E:\\test\\test\\11.png",General_OCE_URL);
        //twoPaceOCR("E:\\test\\test\\15.png",DriverLicense_OCE_URL,0);
        //dirverOCR("E:\\test\\test\\timg.jpg",DriverLicense_OCE_URL,0);
    }

    /**  通用ocr 识别 */
    public static void onePaceOCR(String str,String url){
        try {
            Map<String,String> headerMap=new HashMap<>();
            headerMap.put("Content-Type","application/x-www-form-urlencoded");
            headerMap.put("User-Agent","okhttp/3.10.0");
            headerMap.put("Accept-Encoding","gzip");

            String imageBase64 = Base64.encodeToString(FileHelper.getBytesFromFile(new File(str)), 2);
            String salt = "" + new Random().hashCode();
            String timeStamp = String.valueOf(System.currentTimeMillis()/1000);
            String encodedImage = URLEncoder.encode(imageBase64, "UTF-8");

            String sign = MD5.encrypt( "app_id=" + APP_ID + "&image=" + encodedImage + "&nonce_str=" + salt + "&time_stamp=" + timeStamp + "&app_key=" + APP_KEY).toUpperCase();
            String params=new StringBuilder().append("app_id="+APP_ID)
                    .append("&")
                    .append("time_stamp="+timeStamp)
                    .append("&")
                    .append("nonce_str="+ salt)
                    .append("&")
                    .append("sign="+sign)
                    .append("&")
                    .append("image="+encodedImage).toString();

            String result= HttpUtil.getInstance(false,false).sendPost(url,headerMap,params);
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**  通用ocr 识别 */
    public static void twoPaceOCR(String str,String url,int type){
        try {
            Map<String,String> headerMap=new HashMap<>();
            headerMap.put("Content-Type","application/x-www-form-urlencoded");
            headerMap.put("User-Agent","okhttp/3.10.0");
            headerMap.put("Accept-Encoding","gzip");

            String imageBase64 = Base64.encodeToString(FileHelper.getBytesFromFile(new File(str)), 2);
            String salt = "" + new Random().hashCode();
            String timeStamp = String.valueOf(System.currentTimeMillis()/1000);
            String encodedImage = URLEncoder.encode(imageBase64, "UTF-8");

            String sign = MD5.encrypt( "app_id=" + APP_ID + "&card_type="+type+"&image=" + encodedImage + "&nonce_str=" + salt + "&time_stamp=" + timeStamp + "&app_key=" + APP_KEY).toUpperCase();
            String params=new StringBuilder().append("app_id="+APP_ID)
                    .append("&")
                    .append("time_stamp="+timeStamp)
                    .append("&")
                    .append("nonce_str="+ salt)
                    .append("&")
                    .append("sign="+sign)
                    .append("&")
                    .append("image="+encodedImage)
                    .append("&")
                    .append("card_type="+type)
                    .toString();

            String result= HttpUtil.getInstance(false,false).sendPost(url,headerMap,params);
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**  通用ocr 识别 */
    public static void dirverOCR(String str,String url,int type){
        try {
            Map<String,String> headerMap=new HashMap<>();
            headerMap.put("Content-Type","application/x-www-form-urlencoded");
            headerMap.put("User-Agent","okhttp/3.10.0");
            headerMap.put("Accept-Encoding","gzip");

            String imageBase64 = Base64.encodeToString(FileHelper.getBytesFromFile(new File(str)), 2);
            String salt = "" + new Random().hashCode();
            String timeStamp = String.valueOf(System.currentTimeMillis()/1000);
            String encodedImage = URLEncoder.encode(imageBase64, "UTF-8");

            String sign = MD5.encrypt( "app_id=" + APP_ID + "&image=" + encodedImage + "&nonce_str=" + salt + "&time_stamp=" + timeStamp + "&type="+type+"&app_key=" + APP_KEY).toUpperCase();
            String params=new StringBuilder().append("app_id="+APP_ID)
                    .append("&")
                    .append("time_stamp="+timeStamp)
                    .append("&")
                    .append("nonce_str="+ salt)
                    .append("&")
                    .append("sign="+sign)
                    .append("&")
                    .append("image="+encodedImage)
                    .append("&")
                    .append("type="+type)
                    .toString();

            String result= HttpUtil.getInstance(false,false).sendPost(url,headerMap,params);
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }



}
